package com.example.service.impl;

import com.example.dao.OrderDao;
import com.example.dao.TripDao;
import com.example.dao.impl.OrderDaoImpl;
import com.example.dao.impl.TripDaoImpl;
import com.example.model.Trip;
import com.example.service.TripService;

import java.util.List;

public class TripServiceImpl implements TripService {
    private TripDao tripDao = new TripDaoImpl();
    private OrderDao orderDao = new OrderDaoImpl();
    @Override
    public boolean addTrip(Trip trip) {
        Trip trip_exist = tripDao.findTripById(trip.getId());
        if (null != trip_exist) {
            // 该车次存在
            return false;
        }
        boolean flag = tripDao.addTrip(trip);
        return flag;
    }

    @Override
    public List<Trip> booking() {
        return tripDao.findAll();
    }

    @Override
    public boolean purcharse(String id ,int u_id) {
        // 查询当前是否还有票
        Trip trip = tripDao.findTripById(id);
        if (trip == null || trip.getCount() <= 0) {
            return false;
        }
        int row = tripDao.updateCount(id);
        if (row <= 0) {
            return false;
        }
        // 将订单信息添加到订单表中
        row = orderDao.addOrder(u_id, id);
        if (row <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean concele(String id, int uid) {
        int row = tripDao.updateCount(id,-1);
        if (row <= 0) {
            return false;
        }
        // 删除订单信息
        row = orderDao.deleteOrder(uid, id);
        if (row <= 0) {
            return false;
        }
        return true;
    }
}
